﻿@using A2.ERP.Models
@{
    List<t_ProductsBrand> productsBrandList = ViewBag.productsBrand;
}

<section id="SysProductsBrandManager">
    <div class="card">
        <div class="card-header">添加群组</div>
        <div class="card-body">
            @using (Html.BeginForm("SysProductsBrandManager", "Setting", FormMethod.Post, new
            {
                @class = "form addProductsBrand"
            }))
            {
                <div class="form-group">
                    <label>品牌</label>
                    <input type="text" name="brandname" value="" class="form-control" />
                </div>
                <div class="form-group">
                    <label>状态</label>
                    <div>
                        <input type="radio" name="state" value="Y" />
                        启用
                        <input type="radio" name="state" value="N" />
                        禁用
                    </div>
                </div>
                <div class="form-group">
                    <label>备注</label>
                    <input type="text" name="description" value="" class="form-control" />
                </div>
                <div class="form-group">
                    <button class="btn btn-primary" type="submit">添加</button>
                </div>
            }

        </div>
    </div>
    <table class="table table-bordered">
        <thead>
            <tr>
                <th>编号</th>
                <th>名称</th>
                <th>描述</th>
                <th>状态</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var productsBrand in productsBrandList)
            {
                <tr>
                    <td>@productsBrand.BrandID</td>
                    <td>@productsBrand.BrandName</td>
                    <td>
                        @productsBrand.Description
                    </td>
                    <td>
                        @Html.Raw(productsBrand.State == "Y"
                       ? @"<i class='icon SkyUI icon-check-circle text-success'></i>"
                        : @"<i class='icon SkyUI icon-close-circle text-danger'></i>")
                    </td>
                    <td>
                        <button class="btn btn-sm btn-primary btn-updateProductsBrand" data-id="@productsBrand.BrandID" data-target="#updateProductsBrandModal" data-toggle="modal">
                            <i class="icon SkyUI icon-search"></i>
                            编辑
                        </button>
                        <button class="btn btn-sm btn-danger btn-delProductsBrand" data-id="@productsBrand.BrandID" data-target="#delProductsBrandModal" data-toggle="modal">
                            <i class="icon SkyUI icon-close"></i>
                            删除
                        </button>
                    </td>
                </tr>
            }
        </tbody>
    </table>

    <div class="modal fade" id="delProductsBrandModal" tabindex="-1" role="dialog" aria-labelledby="sysProductsBrandModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">确定要删除该品牌吗？</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    确定要删除该品牌吗?
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                    <button type="button" class="btn btn-primary btn-sureDelProductsBrand">确定</button>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade" id="updateProductsBrandModal" tabindex="-1" role="dialog" aria-labelledby="updatesysProductsBrandModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">修改品牌</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    @using (Html.BeginForm("EditProductsBrand", "Setting", FormMethod.Post, new
                    {
                        @class = "editProductsFrm"
                    }))
                    {
                        <div class="form-group">
                            <input type="text" name="BrandID" value="" readonly style="display:none;" />
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <label class="input-group-text">品牌</label>
                                </div>
                                <input type="text" name="BrandName" value="" class="form-control" />
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="input-group">
                                <div class="input-group-prepend">
                                    <label class="input-group-text">备注</label>
                                </div>
                                <input type="text" name="Description" value="" class="form-control" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label>状态</label>
                            <div>
                                <input type="radio" name="state" value="Y" checked />
                                启用
                                <input type="radio" name="state" value="N" />
                                禁用
                            </div>
                        </div>
                        <div>
                            <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
                            <button class="btn-primary btn">修改</button>
                        </div>
                    }
                </div>
            </div>
        </div>
    </div>
</section>

<script>
    ;
    !function () {
        var $addProductsBrandFrm = $('#SysProductsBrandManager .addProductsBrand');
        var $updateProductsBrandFrm = $('#SysProductsBrandManager .editProductsFrm');
        var $btnDelProductsBrand = $('#SysProductsBrandManager .btn-delProductsBrand');
        var $btnUpdateProductsBrand = $('#SysProductsBrandManager .btn-updateProductsBrand');
        var $btnSureDelProductsBrand = $('#SysProductsBrandManager .btn-sureDelProductsBrand');

        var currentId = null;

        $btnDelProductsBrand.on('click', function () {
            currentId = $(this).attr('data-id');
        });

        $btnUpdateProductsBrand.on('click', function () {
            currentId = $(this).attr('data-id');

            $('.editProductsFrm input[name=BrandID]').val(currentId);

            $.get({
                url: '/setting/EditProductsBrand?id=' + currentId,
                type: 'GET',
                dataType: 'json',
                success: function (data) {
                    $('.editProductsFrm input[name=BrandName]').val(data.BrandName);
                    $('.editProductsFrm input[name=Description]').val(data.Description);
                }
            })
        });

        $btnSureDelProductsBrand.on('click', function () {
            //debugger;
            $.ajax({
                url: '/setting/SysProductsBrandManager',
                dataType: 'json',
                type: 'DELETE',
                data: { id: currentId },
                success: function (data) {
                    if (!!data) {
                        $('.modal').modal('hide');
                        $tab.reloadTab('#SysProductsBrandManager');
                    }
                }
            });
        });

        $updateProductsBrandFrm.submit(function (e) {
            e.preventDefault();

            $updateProductsBrandFrm.ajaxSubmit({
                type: 'PUT',
                dataType: 'json',
                success: function (data) {
                    $('.modal').modal('hide');
                    $tab.reloadTab('#SysProductsBrandManager');
                }
            })
        })

        $addProductsBrandFrm.submit(function (e) {
            e.preventDefault();

            $addProductsBrandFrm.ajaxSubmit({
                type: 'POST',
                dataType: 'json',
                success: function (e) {
                    $tab.reloadTab('#SysProductsBrandManager');
                }
            });
        });
    }();
</script>